package com.irdstudio.bsp.executor.core.plugin.dataexport;

import java.sql.Connection;
import java.util.concurrent.Callable;

import com.irdstudio.bsp.executor.core.tinycore.log.ILogger;

/**
 * 
 * TODO 线程处理导数任务，使用Callable
 * 
 * @author Mr.Fang
 * @email luck_2088@163.com
 * @date 2019年1月21日 下午8:21:43
 *
 */
public class DataExportCallable implements Callable<Boolean> {

	/* 数据装载配置信息 */
	private PluginExportConf exportConf = null;
	// 数据库连接
	private Connection conn = null;
	/* 文件日志记录者 */
	private ILogger logger = null;

	public DataExportCallable() {
	}

	public DataExportCallable(PluginExportConf exportConf, Connection conn, ILogger logger) {
		this.conn = conn;
		this.exportConf = exportConf;
		this.logger = logger;
	}

	@Override
	public Boolean call() throws Exception {
		boolean flag = true;
		long startTime = System.currentTimeMillis();
		try {
			String exportToFile = exportConf.getExportToFile();

			logger.info("线程[" + Thread.currentThread().getName() + "]执行拆分导出数据[" + exportToFile + "]");
			// 卸数处理类
			DataExportExecutor dataExport = new DataExportExecutor(exportConf, conn, logger);
			flag = dataExport.run();

		} catch (Exception e) {
			flag = false;
			throw new Exception(e);
		} finally {
			if (conn != null) {
				conn.close();
			}
			logger.info("线程[" + Thread.currentThread().getName() + "]执行拆分导出结束[" + flag + "],用时["
					+ (System.currentTimeMillis() - startTime) / 1000 + "秒]。");
		}
		return flag;
	}

	public Connection getConn() {
		return conn;
	}

	public void setConn(Connection conn) {
		this.conn = conn;
	}

	public PluginExportConf getExportConf() {
		return exportConf;
	}

	public void setExportConf(PluginExportConf exportConf) {
		this.exportConf = exportConf;
	}

	public ILogger getLogger() {
		return logger;
	}

	public void setLogger(ILogger logger) {
		this.logger = logger;
	}
}
